package com.hong.activemq;


import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author: csh
 * @Date: 2021/4/19 15:05
 * @Description:active生产
 */
public class Producer {
    //地址
    public static final String URL = "tcp://localhost:61616";
    //队列名称
    public static final String queueName = "QUEUE_HONG";

    public static void main(String args[]) throws JMSException {
        //1.创建ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(URL);
        //2.创建Connection
        Connection connection = connectionFactory.createConnection();
        //3.启动连接
        connection.start();
        //4.创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5.创建一个目标
        Destination destination = session.createQueue(queueName);
        //6.创建一个生产者
        MessageProducer producer = session.createProducer(destination);
        for (int i = 0 ;i < 10; i++){
            //7.创建消息
            TextMessage message = session.createTextMessage("hello I'm hong:"+i);
            //8.发送消息
            producer.send(message);
            System.out.println("发送消息:"+i+"成功!");
        }
        //9.关闭连接
        connection.close();
}
}
